<template>
  <el-dialog
    :title="!dataForm.auditId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="关联请假条字段" prop="auditRecordId">
      <el-input v-model="dataForm.auditRecordId" placeholder="关联请假条字段"></el-input>
    </el-form-item>
    <el-form-item label="请假人姓名" prop="restName">
      <el-input v-model="dataForm.restName" placeholder="请假人姓名"></el-input>
    </el-form-item>
    <el-form-item label="审批人id" prop="auditPeopleId">
      <el-input v-model="dataForm.auditPeopleId" placeholder="审批人id"></el-input>
    </el-form-item>
    <el-form-item label="请假人姓名" prop="restId">
      <el-input v-model="dataForm.restId" placeholder="请假人姓名"></el-input>
    </el-form-item>
    <el-form-item label="审批人姓名" prop="auditPeopleName">
      <el-input v-model="dataForm.auditPeopleName" placeholder="审批人姓名"></el-input>
    </el-form-item>
    <el-form-item label="审批状态-0是未通过1是通过2是驳回" prop="auditStatus">
      <el-input v-model="dataForm.auditStatus" placeholder="审批状态-0是未通过1是通过2是驳回"></el-input>
    </el-form-item>
    <el-form-item label="通过或者驳回原因" prop="reason">
      <el-input v-model="dataForm.reason" placeholder="通过或者驳回原因"></el-input>
    </el-form-item>
    <el-form-item label="审批时间" prop="auditTime">
      <el-input v-model="dataForm.auditTime" placeholder="审批时间"></el-input>
    </el-form-item>
    <el-form-item label="请假审核阶段-0是未审批1是班主任审批2是书记审批" prop="auditLevel">
      <el-input v-model="dataForm.auditLevel" placeholder="请假审核阶段-0是未审批1是班主任审批2是书记审批"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          auditId: 0,
          auditRecordId: '',
          restName: '',
          auditPeopleId: '',
          restId: '',
          auditPeopleName: '',
          auditStatus: '',
          reason: '',
          auditTime: '',
          auditLevel: ''
        },
        dataRule: {
          auditRecordId: [
            { required: true, message: '关联请假条字段不能为空', trigger: 'blur' }
          ],
          restName: [
            { required: true, message: '请假人姓名不能为空', trigger: 'blur' }
          ],
          auditPeopleId: [
            { required: true, message: '审批人id不能为空', trigger: 'blur' }
          ],
          restId: [
            { required: true, message: '请假人姓名不能为空', trigger: 'blur' }
          ],
          auditPeopleName: [
            { required: true, message: '审批人姓名不能为空', trigger: 'blur' }
          ],
          auditStatus: [
            { required: true, message: '审批状态-0是未通过1是通过2是驳回不能为空', trigger: 'blur' }
          ],
          reason: [
            { required: true, message: '通过或者驳回原因不能为空', trigger: 'blur' }
          ],
          auditTime: [
            { required: true, message: '审批时间不能为空', trigger: 'blur' }
          ],
          auditLevel: [
            { required: true, message: '请假审核阶段-0是未审批1是班主任审批2是书记审批不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.auditId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.auditId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/audit/info/${this.dataForm.auditId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.auditRecordId = data.audit.auditRecordId
                this.dataForm.restName = data.audit.restName
                this.dataForm.auditPeopleId = data.audit.auditPeopleId
                this.dataForm.restId = data.audit.restId
                this.dataForm.auditPeopleName = data.audit.auditPeopleName
                this.dataForm.auditStatus = data.audit.auditStatus
                this.dataForm.reason = data.audit.reason
                this.dataForm.auditTime = data.audit.auditTime
                this.dataForm.auditLevel = data.audit.auditLevel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/audit/${!this.dataForm.auditId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'auditId': this.dataForm.auditId || undefined,
                'auditRecordId': this.dataForm.auditRecordId,
                'restName': this.dataForm.restName,
                'auditPeopleId': this.dataForm.auditPeopleId,
                'restId': this.dataForm.restId,
                'auditPeopleName': this.dataForm.auditPeopleName,
                'auditStatus': this.dataForm.auditStatus,
                'reason': this.dataForm.reason,
                'auditTime': this.dataForm.auditTime,
                'auditLevel': this.dataForm.auditLevel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
